//
//  CheckDataTool.m
//  Stars
//
//  Created by 魏鹏 on 16/12/24.
//  Copyright © 2016年 hst. All rights reserved.
//

#import "CheckDataTool.h"

@implementation CheckDataTool


#pragma mark - 根据项目要求重写验证的规则

// 验证 -
+(BOOL)checkCharRung:(NSString *)chart{
//    /^[\+\-\(\)0-9]{0,40}$/
    NSString *regEx = @"^[\\+\\-\\(\\)\\d]{0,40}";
    return [self baseCheckForRegEx:regEx data:chart];
    
}

#pragma mark - 邮箱校验
+(BOOL)checkForEmail:(NSString *)email{
    
    NSString *regEx = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    return [self baseCheckForRegEx:regEx data:email];
}

#pragma mark - 验证手机号
+(BOOL)checkForMobilePhoneNo:(NSString *)mobilePhone{
    
    NSString *regEx = @"^1[3|4|5|7|8][0-9]\\d{8}$";
    return [self baseCheckForRegEx:regEx data:mobilePhone];
}
#pragma mark - 验证电话号
+(BOOL)checkForPhoneNo:(NSString *)phone{
    NSString *regEx = @"^(\\d{3,4}-)\\d{7,8}$";
    return [self baseCheckForRegEx:regEx data:phone];
}

#pragma mark - 身份证号验证
+ (BOOL) checkForIdCard:(NSString *)idCard{
    
    NSString *regEx = @"(^[0-9]{15}$)|([0-9]{17}([0-9]|X)$)";
    return [self baseCheckForRegEx:regEx data:idCard];
}
#pragma mark - 密码校验
+(BOOL)checkForPasswordWithShortest:(NSInteger)shortest longest:(NSInteger)longest password:(NSString *)pwd{
    NSString *regEx =[NSString stringWithFormat:@"^[a-zA-Z0-9]{%ld,%ld}+$", shortest, longest];
    return [self baseCheckForRegEx:regEx data:pwd];
}

//----------------------------------------------------------------------

#pragma mark - 由数字和26个英文字母组成的字符串
+ (BOOL) checkForNumberAndCase:(NSString *)data{
    NSString *regEx = @"^[A-Za-z0-9]+$";
    return [self baseCheckForRegEx:regEx data:data];
}

#pragma mark - 小写字母
+(BOOL)checkForLowerCase:(NSString *)data{
    NSString *regEx = @"^[a-z]+$";
    return [self baseCheckForRegEx:regEx data:data];
}

#pragma mark - 大写字母
+(BOOL)checkForUpperCase:(NSString *)data{
    NSString *regEx = @"^[A-Z]+$";
    return [self baseCheckForRegEx:regEx data:data];
}
#pragma mark - 26位英文字母
+(BOOL)checkForLowerAndUpperCase:(NSString *)data{
    NSString *regEx = @"^[A-Za-z]+$";
    return [self baseCheckForRegEx:regEx data:data];
}

#pragma mark - 特殊字符
+ (BOOL) checkForSpecialChar:(NSString *)data{
    NSString *regEx = @"[^%&',;=?$\x22]+";
    return [self baseCheckForRegEx:regEx data:data];
}
#pragma mark - 只能是0～100的整数和两位小数
+(BOOL)checkNumZeroAndOneHundred:(NSString *)number{
    if (number.length>=1 && number.judgeStringIsZero) {
        return NO;
    }
    if ([number floatValue]<0 || [number floatValue]>100) {
        return NO;
    }
    
    NSString *regEx = @"^[0-9]+(.[0-9]{1,2})?$";
    return [self baseCheckForRegEx:regEx data:number];
}

//验证100以内的数字，附带参数为小数位
+(BOOL)checkNumZeroAndOneHundred:(NSString *)number byDecimalLength:(NSInteger) length{
    if (number.length >=1 && number.judgeStringIsZero) {
        return NO;
    }
    if ([number floatValue] < 0 || [number floatValue] >100) {
        return NO;
    }
    NSString *regEx = [NSString stringWithFormat:@"^[0-9]+(.[0-9]{1,%ld})?$",length];
    return [self baseCheckForRegEx:regEx data:number];
}

+(BOOL)checkNumZeroToOneHundred:(NSString *)number{
    if ([number floatValue]<=0 || [number floatValue]>100) {
        return NO;
    }
    
    NSString *regEx = @"^[0-9]+(.[0-9]{1,2})?$";
    return [self baseCheckForRegEx:regEx data:number];
}


+(BOOL)checkNumZeroToOneHundred:(NSString *)number byDecimalLength:(NSInteger) length{
    if ([number floatValue]<=0 || [number floatValue]>=100) {
        return NO;
    }
    NSString *regEx = [NSString stringWithFormat:@"^[0-9]+(.[0-9]{1,%ld})?$",length];
//    NSString *regEx = @"^[0-9]+(.[0-9]{1,2})?$";
    return [self baseCheckForRegEx:regEx data:number];
}

+(BOOL)checkNumZeroAndFive:(NSString *)number{

    if (number.length>=1 && number.judgeStringIsZero) {
        return NO;
    }
    
    if ([number floatValue]<0 || [number floatValue]>5) {
        return NO;
    }
    
    NSString *regEx = @"^[0-9]+(.[0-9]{1,2})?$";
    return [self baseCheckForRegEx:regEx data:number];
    
}

+(BOOL)checkNumAnd_thirteen_four:(NSString *)number{
    if ([number isEqualToString:@"0"]) {
        return YES;
    }
    if (number.length>1 &&  [CheckDataTool checkJudgeStringIsZeroWithStr:number]) {
        return NO;
    }
    if ([number doubleValue]<0 || [number doubleValue]>9999999999999.9999) {
        return NO;
    }
    NSString *regEx = @"^[0-9]+(.[0-9]{1,4})?$";
    return [self baseCheckForRegEx:regEx data:number];
}
+(BOOL)checkNumAnd_nine_four:(NSString *)number{
    if ([number isEqualToString:@"0"]) {
        return YES;
    }
    if (number.length>1 && [CheckDataTool checkJudgeStringIsZeroWithStr:number]) {
        return NO;
    }
    if ([number doubleValue]<0 || [number doubleValue]>999999999.9999) {
        return NO;
    }
    NSString *regEx = @"^[0-9]+(.[0-9]{1,4})?$";
    return [self baseCheckForRegEx:regEx data:number];
}

+(BOOL)checkNum:(NSString *)number ByIntegerLength:(NSInteger )InLen ByDecimalsLength:(NSInteger)deLen{
    
    if ([number isEqualToString:@"0"]) {
        return YES;
    }
    if (number.length>1 && [CheckDataTool checkJudgeStringIsZeroWithStr:number]) {
        return NO;
    }
    NSMutableString *muStr = [NSMutableString stringWithString:@""];
    for (NSInteger index = 0; index < InLen; index++) {
        [muStr appendString:@"9"];
    }
    if (deLen > 0) {
        [muStr appendString:@"."];
        for (NSInteger deIndex = 0; deIndex < deLen; deIndex++) {
            [muStr appendString:@"9"];
        }
    }
    
    if ([number doubleValue] < 0 || [number doubleValue] > [muStr doubleValue]) {
        return NO;
    }
    
    
    
    NSString *regEx = [NSString stringWithFormat:@"^[0-9]+(.[0-9]{1,%ld})?$",deLen];
    return [self baseCheckForRegEx:regEx data:number];
    
    return NO;
}


+(BOOL)checkNumAnd_six_four:(NSString *)number{
    if (number.length>1 && number.judgeStringIsZero) {
        return NO;
    }
    if ([number doubleValue]<0 || [number doubleValue]>999999.9999) {
        return NO;
    }
    NSString *regEx = @"^[0-9]+(.[0-9]{1,4})?$";
    return [self baseCheckForRegEx:regEx data:number];
}
#pragma mark - 只能输入数字
+ (BOOL) checkForNumber:(NSString *)number{
    NSString *regEx = @"^[0-9]*$";
    return [self baseCheckForRegEx:regEx data:number];
}

#pragma mark - 校验只能输入n位的数字
+ (BOOL) checkForNumberWithLength:(NSString *)length number:(NSString *)number{
    NSString *regEx = [NSString stringWithFormat:@"^\\d{%@}$", length];
    return [self baseCheckForRegEx:regEx data:number];
}


#pragma mark -  电话号码格式支持所有特殊字符和数字
+(BOOL)checkPhone:(NSString *)data{
    NSString *regEx = @"^[^A-Za-z\u4E00-\u9FA5]+$";
    return [self baseCheckForRegEx:regEx data:data];
}



#pragma mark - 私有方法
/**
 *  基本的验证方法
 *
 *  @param regEx 校验格式
 *  @param data  要校验的数据
 *
 *  @return YES:成功 NO:失败
 */
+(BOOL)baseCheckForRegEx:(NSString *)regEx data:(NSString *)data{
    
    NSPredicate *card = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regEx];
    
    if (([card evaluateWithObject:data])) {
        return YES;
    }
    return NO;
}

+ (BOOL)checkJudgeStringIsZeroWithStr:(NSString*)str{
    
    NSArray *arr = [str componentsSeparatedByString:@"."];
    if (arr.count == 2 ) {//含有小数
        
        while ([str hasSuffix:@"0"]) {
            str = [str substringToIndex:[str length]-1];
        }
        
        if ([str hasSuffix:@"."]) {
            str = [str substringToIndex:[str length]-1];
        }
        
        if ([str isEqualToString:@"0"] || str.length == 0)  return NO;
       
        
        while ([str hasSuffix:@"0"]) {
            str = [str substringToIndex:[str length]-1];
        }
        
        if ([str isEqualToString:@"0"] || str.length == 0) return YES;
       
        
    }else if (arr.count == 1){//不含小数
        while ([str hasSuffix:@"0"]) {
            str = [str substringToIndex:[str length]-1];
        }
        if ([str isEqualToString:@"0"] || str.length == 0) return YES;
    }
    
    
    return NO;
    
}

+ (BOOL)checkForWeChatAccount:(NSString *)str {
    NSString *regEx = @"^[a-zA-Z0-9_-]+$";
    return [self baseCheckForRegEx:regEx data:str];
}

@end
